package com.ruoyi.fun.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fun.mapper.EcCartMapper;
import com.ruoyi.fun.domain.EcCart;
import com.ruoyi.fun.service.IEcCartService;

/**
 * 购物车Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@Service
public class EcCartServiceImpl implements IEcCartService 
{
    @Autowired
    private EcCartMapper ecCartMapper;

    /**
     * 查询购物车
     * 
     * @param cartId 购物车主键
     * @return 购物车
     */
    @Override
    public EcCart selectEcCartByCartId(Long cartId)
    {
        return ecCartMapper.selectEcCartByCartId(cartId);
    }

    /**
     * 查询购物车列表
     * 
     * @param ecCart 购物车
     * @return 购物车
     */
    @Override
    public List<EcCart> selectEcCartList(EcCart ecCart)
    {
        System.out.println("[DEBUG] EcCartServiceImpl.selectEcCartList - 查询参数 orgId: " + ecCart.getOrgId());
        List<EcCart> result = ecCartMapper.selectEcCartList(ecCart);
        System.out.println("[DEBUG] EcCartServiceImpl.selectEcCartList - 查询结果数量: " + result.size());
        for (EcCart cart : result) {
            System.out.println("[DEBUG] EcCartServiceImpl.selectEcCartList - 购物车ID: " + cart.getCartId() + ", orgId: " + cart.getOrgId());
        }
        return result;
    }

    /**
     * 新增购物车
     * 
     * @param ecCart 购物车
     * @return 结果
     */
    @Override
    public int insertEcCart(EcCart ecCart)
    {
        ecCart.setCreateTime(DateUtils.getNowDate());
        // orgId应该由Controller层传入，不在Service层设置默认值
        return ecCartMapper.insertEcCart(ecCart);
    }

    /**
     * 修改购物车
     * 
     * @param ecCart 购物车
     * @return 结果
     */
    @Override
    public int updateEcCart(EcCart ecCart)
    {
        ecCart.setUpdateTime(DateUtils.getNowDate());
        return ecCartMapper.updateEcCart(ecCart);
    }

    /**
     * 批量删除购物车
     * 
     * @param cartIds 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteEcCartByCartIds(Long[] cartIds)
    {
        return ecCartMapper.deleteEcCartByCartIds(cartIds);
    }
    
    /**
     * 批量删除购物车（带组织ID）
     * 
     * @param cartIds 需要删除的购物车主键
     * @param orgId 组织ID
     * @return 结果
     */
    @Override
    public int deleteEcCartByCartIds(Long[] cartIds, Long orgId)
    {
        return ecCartMapper.deleteEcCartByCartIdsAndOrgId(cartIds, orgId);
    }

    /**
     * 删除购物车信息
     * 
     * @param cartId 购物车主键
     * @return 结果
     */
    @Override
    public int deleteEcCartByCartId(Long cartId)
    {
        return ecCartMapper.deleteEcCartByCartId(cartId);
    }
    
    /**
     * 删除购物车信息（带组织ID）
     * 
     * @param cartId 购物车主键
     * @param orgId 组织ID
     * @return 结果
     */
    @Override
    public int deleteEcCartByCartId(Long cartId, Long orgId)
    {
        return ecCartMapper.deleteEcCartByCartIdAndOrgId(cartId, orgId);
    }

    /**
     * 根据cartId和组织ID查询Cart
     * 
     * @param cartId Cart主键
     * @param orgId 组织ID
     * @return Cart
     */
    @Override
    public EcCart selectEcCartByCartIdAndOrgId(Long cartId, Long orgId)
    {
        return ecCartMapper.selectEcCartByCartIdAndOrgId(cartId, orgId);
    }

    /**
     * 根据组织ID查询Cart列表
     * 
     * @param eccart Cart
     * @param orgId 组织ID
     * @return Cart集合
     */
    @Override
    public List<EcCart> selectEcCartListByOrgId(EcCart eccart, Long orgId)
    {
        return ecCartMapper.selectEcCartListByOrgId(eccart, orgId);
    }
}